Concurrent, Real-Time and Distributed Programming in Java by Badr Benmammar
Author:Badr Benmammar [Неизв.]
Language: eng
Format: epub
ISBN: 9781119482758
Publisher: John Wiley & Sons, Inc.
Published: 2017-12-13T00:00:00+00:00
Execution:
Server side:
connected
received: A
Client side:
server -> client: Student: A GL: 13
4.2.1.8. Communications between a Java applet and a server using sockets
The following execution shows a communication between an applet and a server using sockets:
Figure 4.4. Communication between an applet and a server with the Sockets. For a color version of the figure, see www.iste.co.uk/benmammar/java.zip
The implementation of the server is as follows:
import java.io.*; import java.net.*; import java.util.*; public class NetServer { public static void main(String[] args) { try{ ServerSocket serverSocket = new ServerSocket(8765); Socket clientSocket = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); while (true) { Integer numb = new Integer(in.readLine()); System.out.println("recu: " + numb.intValue()); out.println(numb.intValue()+1); } }catch(IOException ex){System.err.println(ex);} } }
The implementation of the client is as follows:
import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class NetApplet extends Applet implements ActionListener { TextField numbField; Label display; Socket socket; public void init() { try { socket = new Socket("localhost",8765); } catch (UnknownHostException e) {System.out.println("Unknown host");} catch (IOException e) { System.out.println("IO Exception");} numbField = new TextField(6); add(numbField); Button button = new Button("Send"); add(button); button.addActionListener(this); display = new Label("Pas de nombre"); add(display); } public void actionPerformed(ActionEvent e) { int numb = 0; String numbStr = null; BufferedReader in = null; PrintWriter out = null; String actionCommand = e.getActionCommand(); // know the source of the event if (e.getSource() instanceof Button) if (actionCommand.equals("Send")){ try {numb = Integer.parseInt(numbField.getText());} catch (NumberFormatException ex) {System.out.println ("Number Format Exception");} try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); } catch (IOException ex) {System.out.println("IO Exception");} out.println(numb); try {numbStr = in.readLine();} catch (IOException ex) {System.out.println("Applet receive failed:");} display.setText(numbStr); } } }// end of class
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27113)
Hello! Python by Anthony Briggs(25968)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25304)
Kotlin in Action by Dmitry Jemerov(24413)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23607)
Dependency Injection in .NET by Mark Seemann(23326)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21961)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20865)
Grails in Action by Glen Smith Peter Ledbrook(19880)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17079)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16845)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14472)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12592)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11875)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10655)
Hit Refresh by Satya Nadella(9244)
The Kubernetes Operator Framework Book by Michael Dame(8591)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8449)
Robo-Advisor with Python by Aki Ranin(8392)